package Stack;

import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-05
 * Time:14:15
 * Text：946. 验证栈序列
 */
public class Text5 {
    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> t1=new Stack<>();
        t1.push(10000);
        int j=0;
        for(int i=0;i<pushed.length;){
            if(t1.peek()!=popped[j]){
                t1.push(pushed[i++]);
            }else{
                t1.pop();
                j++;
            }
        }
        while(t1.size()>1){
            if(t1.pop()!=popped[j++]){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        int[] arrs={4,5,3,2,1};
        validateStackSequences(arr,arrs);
    }
}
